/**
* 回车添加标签的事件绑定
*/
function add_tag_bind() {
	$("#current_input").keydown(function(e) {
		if(e.keyCode == 13) {
			var tag_name = $("#current_input").val();
			if(tag_name == "") return;
			var new_delete_button = $("<a title='删除' class='delete_button'>x</a>");
			new_delete_button.click(function() {
				$(this).parent().remove();
			});
			var new_node = $("<div class='tag'><span>" + tag_name + "</span></div>");
			new_node.append(new_delete_button);
			new_node.hover(function() {
				$(this).css('background-color', '#3366FF');
			}, function() {
				$(this).css('background-color', '#3300CC');
			});
			$(new_node).appendTo("#tags");
			$("#current_input").val("");
		}
	});
}

/**
* 退格键删除标签的事件绑定
*/
function remove_tag_bind() {
	$("#current_input").keydown(function(e) {
		var tag_name = $("#current_input").val();
		if(tag_name != "") return;
		if(e.keyCode == 8) {
			$("#tags").children(":last-child").remove();
		}
	});
}

/**
* 实时监听标签输入框的输入变化
*/ 
function listen_tag_change() {
	var tag_name = $("#current_input").val();
	if(tag_name == "") return;
	var pattern = /;/;
	if(pattern.test(tag_name)) {
		var strs = tag_name.split(";");
		tag_name = strs[0];
		if(tag_name == "") return;
		var new_delete_button = $("<a title='删除' class='delete_button'>x</a>");
		new_delete_button.click(function() {
			$(this).parent().remove();
		});
		var new_node = $("<div class='tag'><span>" + tag_name + "</span></div>");
		new_node.append(new_delete_button);
		new_node.hover(function() {
			$(this).css('background-color', '#3366FF');
		}, function() {
			$(this).css('background-color', '#3300CC');
		});
		$(new_node).appendTo("#tags");
		$("#current_input").val("");
	}
}

/**
* 删除标签按钮的事件绑定
*/
function delete_button_bind() {
	$(".delete_button").click(function() {
		$(this).parent().remove();
	});
}